# Spring 2025 (revision 1 for JPE Micro)
library(readr)
library(data.table)
library(scales)

output <- read_csv("H:/Users/Zheng_10223/Joint/refugeepredictactp25.csv")
outputrealized <- read_csv("H:/Users/Zheng_10223/Joint/dfrefugeep25realized.csv")
output <- output[,2:4]; outputrealized <- outputrealized[,2:4]

output_others <- read_csv("H:/Users/Zheng_10223/Joint/nonrefugeepredictactp25.csv")
output_others_realized <- read_csv("H:/Users/Zheng_10223/Joint/Immigrant IGM/dfnonrefugeep25realized.csv")

output_others <- output_others[,2:4]; output_others_realized <- output_others_realized[,2:4]



# Portrait

par(mar=c(4.5,1.5,3,1.5))
m<-matrix(c(1,2,2,3,3),nrow=1,ncol=5,byrow=TRUE)
layout(mat=m)

plot(x=rep(0,27), y=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), pch="", xaxt="n", yaxt="n", bty="n", xlab="", ylab="")
text(x=-1.2, y=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), adj=0, cex=1.5,
     c("Overall", "Sex: Male", "Sex: Female", "Landing Age: 0-4", "Landing Age: 5-9", "Landing Age: 10-14", "Landing Age: 15-17",
       "Speak English? No", "Speak English: Yes", "Intended Occ. Non-Workers", "Intended Occ. New Workers", "Intended Occ. Clerical & Laborers","Intended Occ. Skilled & Technical",  "Intended Occ. Managerial & Professional",   
       "Birthplace: Europe", "Birthplace: South and Central America", "Birthplace: Oceania and other Asia", "Birthplace: Africa and Middle East", "Birthplace: Southern Asia", "Birthplace: Eastern Asia", "Minority Neighborhood: No","Minority Neighborhood: Yes",
       "Landing CMA: Calgary", "Landing CMA: Edmonton", "Landing CMA: Vancouver", "Landing CMA: Toronto", "Landing CMA: Montreal"), xpd=TRUE)


plot(x=output$fit, y=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), xlim=c(39,59), xlab="Child Rank | Parent=p25", ylab="", yaxt="n",  cex.lab=2, cex.axis=2, main="Refugees", cex.main=2,
     pch=c(19, 7,7, 20,20,20,20, 23,23, 18,18,18,18,18, 15,15,15,15,15,15, 12,12, 17,17,17,17,17), cex=c(1.3,1.3,1.3,2,2,2,2,1.3,1.3,2,2,2,2,2,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3),
     col=c(alpha("black",0.3), rep(alpha("brown", 0.3), 2),rep(alpha("red",0.3), 4), rep(alpha("black", 0.3), 2), rep(alpha("orange", 0.3), 5), rep(alpha("forestgreen", 0.3), 6), rep(alpha("purple", 0.3), 2), rep(alpha("royalblue3", 0.3), 5)))
arrows(y0=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), y1=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), x0=output$lwr, x1=output$upr, length=0, 
       col=c(alpha("black",0.3), rep(alpha("brown", 0.3), 2),rep(alpha("red",0.3), 4), rep(alpha("black", 0.3), 2), rep(alpha("orange", 0.3), 5), rep(alpha("forestgreen", 0.3), 6), rep(alpha("purple", 0.3), 2), rep(alpha("royalblue3", 0.3), 5)))
points(x=outputrealized[,1]$fit, y=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), xlim=c(42,67),xlab="", ylab="", yaxt="n",  cex.lab=1.5, cex.axis=1.5,
       pch=c(19, 7,7, 20,20,20,20, 23,23, 18,18,18,18,18, 15,15,15,15,15,15, 12,12, 17,17,17,17,17), cex=c(1.3,1.3,1.3,2,2,2,2,1.3,1.3,2,2,2,2,2,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3),
       col=c("black", rep("brown", 2), rep("red",4), rep("black", 2), rep("orange", 5), rep("forestgreen", 6),rep("purple",2), rep("royalblue3", 5)))
arrows(y0=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), y1=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), x0=outputrealized$lwr, x1=outputrealized$upr, length=0, 
       col=c("black", rep("brown", 2),rep("red",4), rep("black", 2), rep("orange", 5), rep("forestgreen", 6),rep("purple",2), rep("royalblue3", 5)))
text(x=output[1,1], y=1.7, "Adjusted", cex=1.2)
text(x=outputrealized[1,1], y=1.7, "Realized", col=alpha("black", 0.4), cex=1.2)



plot(x=output_others[,1]$fit, y=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), xlim=c(39,59),xlab="Child Rank | Parent=p25", ylab="", yaxt="n",  cex.lab=2, cex.axis=2, cex.main=2,
       pch=c(19, 7,7, 20,20,20,20, 23,23, 18,18,18,18,18, 15,15,15,15,15,15, 12,12, 17,17,17,17,17), cex=c(1.3,1.3,1.3,2,2,2,2,1.3,1.3,2,2,2,2,2,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3), main="Non-Refugees",
       col=c(alpha("black",0.3), rep(alpha("brown", 0.3), 2),rep(alpha("red",0.3), 4), rep(alpha("black", 0.3), 2), rep(alpha("orange", 0.3), 5), rep(alpha("forestgreen", 0.3), 6), rep(alpha("purple", 0.3), 2), rep(alpha("royalblue3", 0.3), 5)))
arrows(y0=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), y1=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), x0=output_others$lwr, x1=output_others$upr, length=0, 
       col=c(alpha("black",0.3), rep(alpha("brown", 0.3), 2),rep(alpha("red",0.3), 4), rep(alpha("black", 0.3), 2), rep(alpha("orange", 0.3), 5), rep(alpha("forestgreen", 0.3), 6), rep(alpha("purple", 0.3), 2), rep(alpha("royalblue3", 0.3), 5)))
points(x=output_others_realized[,1]$fit, y=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), xlim=c(42,67),xlab="", ylab="", yaxt="n",  cex.lab=1.5, cex.axis=1.5,
       pch=c(19, 7,7, 20,20,20,20, 23,23, 18,18,18,18,18, 15,15,15,15,15,15, 12,12, 17,17,17,17,17), cex=c(1.3,1.3,1.3,2,2,2,2,1.3,1.3,2,2,2,2,2,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3,1.3),
       col=c("black", rep("brown", 2), rep("red",4), rep("black", 2), rep("orange", 5), rep("forestgreen", 6),rep("purple",2), rep("royalblue3", 5)))
arrows(y0=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), y1=c(1, 3:4, 6:9, 11:12, 14:18, 20:25, 27:28, 30:34), x0=output_others_realized$lwr, x1=output_others_realized$upr, length=0, 
       col=c("black", rep("brown", 2),rep("red",4), rep("black", 2), rep("orange", 5), rep("forestgreen", 6),rep("purple",2), rep("royalblue3", 5)))
text(x=output_others[1,1], y=1.7, "Adjusted", cex=1.2)
text(x=output_others_realized[1,1], y=1.7, "Realized", col=alpha("black", 0.4), cex=1.2)

